<?php
class Announcement extends MY_Controller {
 
	function __construct() {
		parent::__construct();
		$this->load->model('announcement_model', '', true);
		$this->load->model('user_model', '', true);
		$this->load->model('association_model', '', true);
		
		$this->load->helper('form');
	}
	
	//防止恶意处理他人新闻
	function _defend($id) {
		if ($id != "") {
    		$temp_author_id = $this->announcement_model->get_author_by_announcement_id($id);
    		if ($temp_author_id != $_SESSION['id']) {
    			show_404();
    			//redirect(BACKEND_ASSOCIATION_ANNOUNCEMENT_LIST_PATH);
    		}
    	}
	}
	 
	function lists() {
		$user_id = $_SESSION['id'];
		$user_confirm_flag = $_SESSION['confirm_FLAG'];
		
		$condition['author_id'] = $user_id;
		$condition['sort_update_time'] = DESC;
		$data['announcement_list'] = $this->announcement_model->search_by_condition($condition);
		$data['remaining_count'] = $this->_get_remaining_announcement_count($user_confirm_flag);
		
		////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
		
		$this->load->view('backend/association/announcement/list', $data);
	}
	
	function preview($id) {
		//防止恶意处理他人新闻
    	$this->_defend($id);
			
		$condition['id'] = $id;
		$data['announcement_detail'] = $this->announcement_model->search_by_condition($condition, TRUE);
		$data['announcement_detail']['poster_name']
			= $this->association_model->_get_association_name_by_employee_id($data['announcement_detail']['poster_id']);
            	
		////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
		
		$this->load->view("backend/association/announcement/preview", $data);
    }
	
	/**
     * 修改或新增新闻
     */
    function edit($id = '') {
    	
    	$user_confirm_flag = $_SESSION['confirm_FLAG'];
    	
    	//防止恶意处理他人新闻
    	$this->_defend($id);
    	
		///////////////////////////////编辑核心程序开始////////////////////////////////
		$data['remaining_count'] = $this->_get_remaining_announcement_count($user_confirm_flag);
    	//判断该用户还能否发布新闻（$id = 0），若剩余量小于1（不能发布），则返回
		if ($data['remaining_count'] < 1) {
			redirect(BACKEND_ASSOCIATION_ANNOUNCEMENT_LIST_PATH);
		}
		
        //------表单初始化程序开始------
        if ($id != '') {
            $data['announcement'] = $this->announcement_model->get_by_id($id);
            $data['announcement']['poster_name']
            	= $this->association_model->_get_association_name_by_employee_id($data['announcement']['poster_id']);
        } else {
            $data['announcement'] = $this->announcement_model->get_fields();
            $poster_condition['category'] = ASSOCIATION_XCB_CID;
			$poster_condition['confirm_FLAG'] = ASSOCIATION_VERIFY_PASS;
			$poster_condition['function_FLAG'] = NOT_FUNCTION;
			$data['announcement']['temp_poster_array'] = $this->association_model->search_by_condition($poster_condition);
        }
        
        ////////////////已登陆用户信息绑定/////////////
		$data['user_info'] = $this->login_user_info();
		
		////////////////用户后台侧栏信息绑定/////////////
		$data['sidebar_info'] = $this->sidebar_info();
		
        $this->load->view('backend/association/announcement/edit', $data);
    }
    
    function save_edit() {
    	//------表单初始化程序结束------
        $this->load->library('form_validation');
        //常规校验
        $this->form_validation->set_rules('id', 'id', 'xss_clean');
        $this->form_validation->set_rules('is_published', 'is_published', 'xss_clean');
        $this->form_validation->set_rules('title', '公告标题', 'required|xss_clean|min_length[2]|max_length[20]');
    	if (empty($_POST['id'])) {
			$this->form_validation->set_rules('poster_id', '作者', 'required');
		}
		$this->form_validation->set_rules('contents', '公告内容', 'required|xss_clean|min_length[10]|max_length[200]');
        
        if ($this->form_validation->run() == FALSE) {
        	$this->edit($_POST['id']);
        	return;
        }
        if (empty($_POST['id'])) {
            $form_data = $this->_get_form_array(array("id"));
        	foreach ($form_data as $k => $v) {
            	if ($k != 'contents') {
					$form_data[$k] = htmlspecialchars($v);
            	}
            }
            $form_data['author_id'] = $_SESSION['id'];
            $form_data['category_id'] = ASSOCIATION_UCID;
            $form_data['create_time'] = time();
            $form_data['update_time'] = time();
            
            $this->announcement_model->save($form_data);
        } else {
            $form_data = $this->_get_form_array();
        	foreach ($form_data as $k => $v) {
            	if ($k != 'contents') {
					$form_data[$k] = htmlspecialchars($v);
            	}
            }
            $form_data['update_time'] = time();
            $this->announcement_model->update($form_data);
        }
        redirect(BACKEND_ASSOCIATION_ANNOUNCEMENT_LIST_PATH);
        ///////////////////////////////编辑核心程序结束////////////////////////////////
    }
    
    /**
     * 发布已有草稿的新闻
     * @param $id
     */
    function publish_draft($id) {
    	//防止恶意处理他人新闻
    	$this->_defend($id);
    	
		$announcement = $this->announcement_model->get_by_id($id);
		if ($announcement['is_published'] == NOT_PUBLISHED) {
    		$announcement['is_published'] = IS_PUBLISHED;
    		$this->announcement_model->update($announcement);
    	}
    	redirect(BACKEND_ASSOCIATION_ANNOUNCEMENT_LIST_PATH);
    }
    
	function delete($id) {
		//防止恶意处理他人新闻
    	$this->_defend($id);
    	
    	$this->announcement_model->delete($id);
    	redirect(BACKEND_ASSOCIATION_ANNOUNCEMENT_LIST_PATH);
    }
    
    function _get_remaining_announcement_count($user_confirm_flag) {
    	$condition['author_id'] = $_SESSION['id'];
    	$count = $this->announcement_model->search_by_condition($condition, "", TRUE);
    	if ($user_confirm_flag != ASSOCIATION_VERIFY_PASS) {
    		$remaining_count = ASSOCIAYION_NEWS_COUNT - $count;
    	} elseif ($user_confirm_flag == ASSOCIATION_VERIFY_PASS) {
    		$remaining_count = CONFIRMED_ASSOCIATION_NEWS_COUNT - $count;
    	}
    	return $remaining_count;
    }
}
?>